package netpoll

import syscall "golang.org/x/sys/windows"

const _DWORD_MAX = 0xffffffff

const _INVALID_HANDLE_VALUE = ^uintptr(0)

var fdl []uintptr

func newListener() (fd int, err error) {
	var fdPtr uintptr
	fdPtr,err = syscall.CreateIoCompletionPort(_INVALID_HANDLE_VALUE, 0, 0, _DWORD_MAX)
	if err != nil {
		return 0, err
	}

	fdl = append(fdl, fdPtr)
	return len(fdl)
}


// CloseFunc is used to hook the close call.
var CloseFunc func(syscall.Handle) error = syscall.Closesocket

// AcceptFunc is used to hook the accept call.
var AcceptFunc func(syscall.Handle, syscall.Handle, *byte, uint32, uint32, uint32, *uint32, *syscall.Overlapped) error = syscall.AcceptEx

// ConnectExFunc is used to hook the ConnectEx call.
var ConnectExFunc func(syscall.Handle, syscall.Sockaddr, *byte, uint32, *uint32, *syscall.Overlapped) error = syscall.ConnectEx

func getHandler(s int) uintptr {
	if len(fdl) < s {
		return _INVALID_HANDLE_VALUE
	}
	return fdl[s-1]
}
